package group4.niit;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

public class JsonParse {
	public InputStream inputStream = null;

	public String contenFile = "";

	public JSONObject jsonObject = null;

	public JsonParse() {

	}

	public JSONObject getJson(String url) {
		try {
			HttpClient client = new DefaultHttpClient();
			HttpGet get = new HttpGet(url);

			HttpResponse response = client.execute(get);
			HttpEntity entity = response.getEntity();

			inputStream = entity.getContent();

		} catch (Exception e) {
			Log.e("UPDATE", "Error: inputStream");
		}

		String line = "";
		StringBuilder builder = new StringBuilder();
		try {
			BufferedReader bufferedReader = new BufferedReader(
					new InputStreamReader(inputStream));

			while ((line = bufferedReader.readLine()) != null) {
				builder.append(line);
			}

			bufferedReader.close();

			contenFile = builder.toString();

		} catch (Exception e) {
			Log.e("UPDATE", "Error: contenFile");
		}

		try {
			jsonObject = new JSONObject(contenFile);
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return jsonObject;
	}

	public String getStringJson(String url) {

		try {
			HttpClient client = new DefaultHttpClient();
			HttpGet get = new HttpGet(url);

			HttpResponse response = client.execute(get);
			HttpEntity entity = response.getEntity();

			inputStream = entity.getContent();

		} catch (Exception e) {
			// TODO: handle exception
		}

		String line = "";
		StringBuilder builder = new StringBuilder();
		try {
			BufferedReader bufferedReader = new BufferedReader(
					new InputStreamReader(inputStream));

			while ((line = bufferedReader.readLine()) != null) {
				builder.append(line);
			}

			bufferedReader.close();

			contenFile = builder.toString();

		} catch (Exception e) {
			// TODO: handle exception
		}

		return contenFile;
	}

	public Bitmap getIconWeather(String http) {
		Bitmap icon = null;
		try {
			URL url = new URL(http);
			InputStream is = url.openStream();

			icon = BitmapFactory.decodeStream(is);

			is.close();

		} catch (Exception e) {
			// TODO: handle exception
		}

		return icon;
	}

}
